<template>
  <div>
    <a-range-picker v-if="status===0" :value="value" @change="onChange" />
    <a-range-picker
      v-if="status===1"
      :value="value"
      :showTime="{ defaultValue: [moment('00:00:00', 'HH:mm:ss'), moment('23:59:59', 'HH:mm:ss')] }"
      format="YYYY-MM-DD HH:mm:ss"
      @change="onChange"
    />
    <a-range-picker
      v-if="status===2"
      :defaultValue="[moment(today, dateFormat), moment(threeMonth, dateFormat)]"
      @change="onChange"
    />
    <a-range-picker
      v-if="status===3"
      :defaultValue="[moment(yesterday, dateFormat), moment(yesterday, dateFormat)]"
      @change="onChange"
    />
  </div>
</template>

<script>
import moment from 'moment'
// 时间选择组件
export default {
  name: 'date',
  props: {
    value: {
      type: Array,
      required: true,
      default: []
    },
    status: {
      type: Number,
      required: false,
      default: 0
    }
  },
  data () {
    this.dateFormat = 'YYYY-MM-DD'
    var today = new Date().toLocaleString().split(' ')[0]
    var yesterday = (new Date(new Date - 1000 * 60 * 60 * 24)).toLocaleString().split(' ')[0]
    var d = new Date()
    d.setMonth(d.getMonth() + 3)
    var threeMonth = d.toLocaleString().split(' ')[0]
    return {
      today: today,
      yesterday: yesterday,
      threeMonth: threeMonth
    }
  },
  mounted () {
    if (this.status === 2) {
      // console.log('diaoyong')
      var start_time = new Date(this.today + ' 00:00:00').getTime() / 1000
      var end_time = new Date(this.threeMonth + ' 23:59:59').getTime() / 1000
      var date = [this.today, this.threeMonth]
      this.$emit('change', {
        date,
        start_time,
        end_time
      })
    } else if (this.status === 3) {
      var start_time = new Date(this.yesterday + ' 00:00:00').getTime() / 1000
      var end_time = new Date(this.yesterday + ' 23:59:59').getTime() / 1000
      var date = [this.yesterday, this.yesterday]
      this.$emit('change', {
        date,
        start_time,
        end_time
      })
    }
  },
  methods: {
    moment,
    onChange(date, [start_time, end_time]) {
      // 默认格式化成时间戳，如果后续需要其他格式 再加就行
      if (this.status === 1) {
        start_time = new Date(start_time).getTime() / 1000
        end_time = new Date(end_time).getTime() / 1000
      } else {
        start_time = new Date(start_time + ' 00:00:00').getTime() / 1000
        end_time = new Date(end_time + ' 23:59:59').getTime() / 1000
      } 
      this.$emit('change', {
        date,
        start_time,
        end_time
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>